package com.editso.haier.controller.admin;

import com.editso.haier.annon.Admin;
import com.editso.haier.annon.RequireLogin;
import com.editso.haier.bean.CommodityBean;
import com.editso.haier.result.Result;
import com.editso.haier.service.CommodityService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Admin
@RestController
@RequestMapping("/commodity")
@RequiresRoles("admin")
public class CommodityController {

    private CommodityService commodityService;

    @Autowired
    public void setCommodityService(CommodityService commodityService) {
        this.commodityService = commodityService;
    }

    @PostMapping
    @RequireLogin
    public Result post(@RequestBody CommodityBean commodity){
        Subject subject = SecurityUtils.getSubject();
        return commodityService.insertCommodity(commodity, subject.getPrincipal().toString());
    }


    @GetMapping
    @RequireLogin
    public Result get(@RequestParam(name = "topic", required = false) Integer topicId, Integer page, Integer size){
        return topicId == null
                ? commodityService.queryAllCommodity(page, size)
                : commodityService.queryTopicCommodity(topicId, page, size);
    }


    @PutMapping
    @RequireLogin
    public Result put(){
        return null;
    }
}
